package com.yanqu.road.server.cmd.activity.summergames.fromcross;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SummerGamesProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;

@Cmd(code = Protocol.S_SUMMER_GAMES_EXCHANGE_APPLY_CHECK_FROM_CROSS, desc = "提交交换请求确认(跨服返回)")
public class SummerGamesApplyExchangeCheckCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        SummerGamesProto.CrossSummerGamesApplyExchangeCheckRespMsg respMsg = SummerGamesProto.CrossSummerGamesApplyExchangeCheckRespMsg.parseFrom(packet.getBytes());

        Property cost = new Property();
        cost.addProperty(respMsg.getPayId(), BigInteger.ONE);
        player.getModule(PreReduceModule.class).restorePreReduce(cost);

        if(respMsg.getRet() == 0) {
            if (player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.SummerGamesActivity, eLogMoneyType.SummerGamesActivityExchangeConsume)) {
                SummerGamesProto.CrossSummerGamesApplyExchangeConfirmReqMsg.Builder crossReqMsg = SummerGamesProto.CrossSummerGamesApplyExchangeConfirmReqMsg.newBuilder();
                crossReqMsg.setActivityId(respMsg.getActivityId());
                crossReqMsg.setApplyId(respMsg.getApplyId());
                player.sendPacket(Protocol.C_SUMMER_GAMES_EXCHANGE_APPLY_CONFIRM, crossReqMsg);
            }
        }else {
            //失败
            player.sendPacket(Protocol.U_SUMMER_GAMES_EXCHANGE_APPLY, SummerGamesProto.SummerGamesApplyExchangeRespMsg.newBuilder().setRet(respMsg.getRet()));
        }

    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
